package com.cardbaobao.cardbabyclient.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.ui.adapter.base.CommonAdapter;
import com.cardbaobao.cardbabyclient.ui.adapter.base.ViewHolder;
import com.cardbaobao.cardbabyclient.model.ImageFolder;
import com.cardbaobao.cardbabyclient.utils.ScreenUtil;

import java.util.List;

/**
 * 显示图片路径列表dialog
 * Created by zad
 * 2017/1/8 13:33
 */

public class PhotosDirDialog extends Dialog {
    /**
     * 文件路径列表
     */
    private ListView mListDir;
    /**
     * 默认选中位置
     */
    private int mSeletPos = 0;
    /**
     * 文件夹集合列表
     */
    private List<ImageFolder> mImageFolders;

    public PhotosDirDialog(Context context, List<ImageFolder> mImageFolders, String mSelectedFileName) {
        super(context, R.style.Theme_Dialog_From_Bottom);
        setContentView(R.layout.dialog_listview_dir);
        this.mImageFolders = mImageFolders;
        initWindow();
        initViews(mSelectedFileName);
        initEvents();
        setCanceledOnTouchOutside(true);
        setCancelable(true);
    }

    private void initWindow() {
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        Context context = getContext();
        params.width = ScreenUtil.getScreenWidth(context);//设置对话框宽度
        params.height = (int) (ScreenUtil.getScreenHeight(context) * 0.7);//设置对话框高度
        params.gravity = Gravity.BOTTOM;//设置window在底部显示
        window.setAttributes(params);
    }

    /**
     * @param mSelectedFileName 被选中的文件名
     */
    private void initViews(String mSelectedFileName) {
        if (!TextUtils.isEmpty(mSelectedFileName)) {
            for (int i = 0, len = mImageFolders.size(); i < len; i++) {
                if (mSelectedFileName.equals(mImageFolders.get(i).getName())) {
                    //文件名相等，设置当前被选中位置
                    mSeletPos = i;
                    break;
                }
            }
        }
        mListDir = (ListView) findViewById(R.id.id_lv_dir);
        mListDir.setAdapter(new CommonAdapter<ImageFolder>(getContext(), mImageFolders, R.layout.dialog_listview_dir_item) {
            @Override
            public void convert(ViewHolder helper, ImageFolder item, int position) {
                helper.setText(R.id.id_dir_item_name, item.getName());
                helper.setImageByUrl(R.id.id_dir_item_image, item.getFirstImagePath());
                helper.setText(R.id.id_dir_item_count, item.getCount() + "张");
                View view = helper.getView(R.id.id_img_selected);
                if (position == mSeletPos) {
                    view.setVisibility(View.VISIBLE);
                } else {
                    view.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    private void initEvents() {
        mListDir.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (onImageDirSelectedListener != null) {
                    mSeletPos = position;
                    onImageDirSelectedListener.onImageDirSelected(mImageFolders.get(position));
                }
            }
        });
    }

    private OnImageDirSelectedListener onImageDirSelectedListener;

    public void setOnImageDirSelectedListener(OnImageDirSelectedListener onImageDirSelectedListener) {
        this.onImageDirSelectedListener = onImageDirSelectedListener;
    }

    public interface OnImageDirSelectedListener {
        void onImageDirSelected(ImageFolder imageFolder);
    }
}
